package com.demo.view;

import com.demo.controller.BackWardListener;

import javax.swing.*;
import java.awt.*;

public class ScoreView extends JPanel {

    private final JLabel score;
    private final JButton backButton;

    private final int width = MainViewPanel.WIDTH;
    private final int height = MainViewPanel.HEIGHT - MainViewPanel.WIDTH;

    private static final int vgap = 10;
    private static final int hgap = 10;
    private final int contentHeight = height - vgap * 2;
    private final int scorePanelWidth = 160;


    public ScoreView() {
        score = new JLabel("0");
        backButton = new JButton();
        setLayout(new FlowLayout(FlowLayout.LEFT, hgap, vgap));
        setBackground(Color.decode("#f39dce"));
        init();
    }

    private void init() {
        initBackButton();
        initScorePanel();
    }

    private void initBackButton() {
        // the panel used to put a back-button
        JPanel panel = new JPanel(null);
        panel.setPreferredSize(new Dimension(width - hgap * 3 - scorePanelWidth, contentHeight));

        String text = "back";
        backButton.setText(text);
        backButton.setBackground(Color.decode("#82b5d2"));
        backButton.setBounds(0, 0,
                width - hgap * 3 - scorePanelWidth, contentHeight);
        backButton.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
        backButton.setForeground(Color.decode("#f8d7f6"));
        backButton.addActionListener(BackWardListener.getInstance());

        panel.add(backButton);
        this.add(panel);
    }

    private void initScorePanel() {
        JPanel scorePanel = new JPanel();
        scorePanel.setPreferredSize(new Dimension(scorePanelWidth, contentHeight));
        scorePanel.setBackground(Color.decode("#82b5d2"));
        scorePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 20));
        Font font = CardsView.getInstance()
                .getJustifyFont("0", scorePanelWidth, "Comic Sans MS", Font.BOLD, 50);
        score.setForeground(Color.decode("#f8d7f6"));
        score.setFont(font);
        scorePanel.add(score);
        this.add(scorePanel);
    }


    public void setScore(int num) {
        Font font = CardsView.getInstance()
                .getJustifyFont("0", scorePanelWidth, "Comic Sans MS", Font.BOLD, 50);
        score.setText(String.valueOf(num));
        score.setFont(font);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }
}
